<?php

namespace App\Model;

use Hyperf\Utils\Arr;

/**
 * App\IotLog
 *
 * @property int $id
 * @property string $iot_sn 物联网编号
 * @property string $type 类型
 * @property string $content 交互内容
 */
class IotLog extends Model
{
    //
    public bool $timestamps = true;

    protected ?string $table = 'iot_logs';
    protected array $fillable = [
        'id','iot_sn','type',
        'content','is_send','send_at'
    ];


    const TYPE_SEND = 'send';
    const TYPE_RECEIVE = 'receive';
    const TYPE_MAP = [
        self::TYPE_SEND => '发送',
        self::TYPE_RECEIVE => '接收',
    ];


    public function getTypeDes()
    {
        return Arr::get(self::TYPE_MAP, $this->type);
    }


    /**
     * @param $iotSn
     * @param $type
     * @param array $params
     */
    public static function record($iotSn, $type, array $params)
    {
        $log = new self();
        $log->iot_sn = $iotSn;
        $log->content = json_encode($params);
        $log->type = $type;
        $log->save();
    }

}
